package com.cpi.cornpp.salesforcast.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.product.exception.ProductException;
import com.cpi.cornpp.product.services.ProductService;
import com.cpi.cornpp.product.view.ProductView;
import com.cpi.cornpp.salesforcast.exception.SalesForecastException;
import com.cpi.cornpp.salesforcast.form.SalesForecastForm;
import com.cpi.cornpp.salesforcast.view.SalesForecastView;

public class ProductListAction extends MappingDispatchAction {
	private final static String DEFAULT = "default";//$NON-NLS-1$
	private static final String FAILURE = "fail";//$NON-NLS-1$
	private static final String SALES_FORECAST_IN_SESSION_LABEL = "SalesForecastView";//$NON-NLS-1$
	private static final String CUSTOMER_ID_LABEL = "customerId";//$NON-NLS-1$
	private static final String CUSTOMER_DESCRIPTION_LABEL = "customerDescription";//$NON-NLS-1$
	private static final String MATERIAL_ID_LABEL = "materialId";//$NON-NLS-1$
	private static final String MATERIAL_DESCRIPTION_LABEL = "materialDescription";//$NON-NLS-1$
	private static final String YEAR_LABEL = "year";//$NON-NLS-1$
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private static final String MESSAGE_SELECT_A_PRODUCT = "error.message.futurepurchase.add.products";//$NON-NLS-1$
	private void saveFutureInSession(HttpServletRequest request){
		SalesForecastView salesForecastView = null;
		HttpSession httpSession = null;
		try{
			httpSession = request.getSession();
			salesForecastView = new SalesForecastView();
			salesForecastView.setCustomer(request.getParameter(CUSTOMER_ID_LABEL));
			salesForecastView.setCustomerDescription(request.getParameter(CUSTOMER_DESCRIPTION_LABEL));
			salesForecastView.setMaterial(request.getParameter(MATERIAL_ID_LABEL));
			salesForecastView.setMaterialDescription(request.getParameter(MATERIAL_DESCRIPTION_LABEL));
			salesForecastView.setFiscalYear(request.getParameter(YEAR_LABEL));
			httpSession.setAttribute(SALES_FORECAST_IN_SESSION_LABEL, salesForecastView);
		}finally{}
	}
	
	public ActionForward goProductSelection(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws ProductException {
		SalesForecastForm salesForecastForm = null;
		ProductView productView = null;
		String productId = null;
		Collection listOfProducts = null;
		ProductService productService = null;
		MessageResources messageResources = null;
		String messageError = null;
		HttpSession httpSession = null;
		try{
			salesForecastForm = (SalesForecastForm)form;
			productService = new ProductService();
			messageResources = getResources(request);
			httpSession = request.getSession();
			this.saveFutureInSession(request);
			productId = request.getParameter(MATERIAL_ID_LABEL);
			productView = new ProductView();
			productView.setProductId(StringUtils.EMPTY);
			if( StringUtils.isEmpty(productId) ){
				productView.setDescription(StringUtils.EMPTY);
				salesForecastForm.setProductViewSearch(productView);
			}else{
				productView.setDescription(productId);
				salesForecastForm.setProductViewSearch(productView);
				listOfProducts = new ArrayList();
				listOfProducts =  productService.getProductList(salesForecastForm.getProductViewSearch());
				request.setAttribute("listOfProducts", listOfProducts);
				salesForecastForm.setResultProductViews(listOfProducts);
			}
		}catch (ProductException productException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), productException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, productException);
				form.reset(mapping, request);
				throw productException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);				
			}
			throw productException;
		}
		catch (Exception exception){
			ProductException productException=null;
			exception.printStackTrace(System.out);
			productException= new ProductException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw productException;
		}
		return mapping.findForward(DEFAULT);
	}
	
	public ActionForward productFutureSearch(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws SalesForecastException, ProductException {
		SalesForecastForm salesForecastForm = null;
		ProductService productService = null;
		Collection listOfProducts = null;
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		String messageError = null;
		try{
			httpSession = request.getSession(false);
			messageResources = getResources(request);
			salesForecastForm = (SalesForecastForm) form;
			productService = new ProductService();
			listOfProducts = new ArrayList();
			listOfProducts =  productService.getProductList(salesForecastForm.getProductViewSearch());
			request.setAttribute("listOfProducts", listOfProducts);
			salesForecastForm.setResultProductViews(listOfProducts);
		}catch (ProductException productException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), productException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, productException);
				form.reset(mapping, request);
				throw productException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);				
			}
			throw productException;
		}
		catch (Exception e){
			SalesForecastException salesForecastException=null;
			e.printStackTrace(System.out);
			salesForecastException= new SalesForecastException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw salesForecastException;
		}
		return mapping.findForward(DEFAULT);
	}
	
	public ActionForward selectProduct(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws SalesForecastException, ProductException {
		SalesForecastForm salesForecastForm = null;
		ProductView productView = null;
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		String messageError = null;
		String forward = null;
		ProductService productService = null;
		try{
			messageResources = getResources(request);
			httpSession = request.getSession(false);
			salesForecastForm = (SalesForecastForm) form;
			if(salesForecastForm.getProductViewSearch().getProductId() != null){
				if (!CollectionUtils.isEmpty(salesForecastForm.getResultProductViews())){
					Iterator iterator = salesForecastForm.getResultProductViews().iterator();
					while (iterator.hasNext()) {
						ProductView view = (ProductView) iterator.next();
						if(org.apache.commons.lang.StringUtils.equals(view.getProductId().trim(), salesForecastForm.getProductViewSearch().getProductId().trim())){
							productView = new ProductView();
							productView.setProductId(view.getProductId().trim());
							productView.setDescription(view.getDescription().trim());
							httpSession = request.getSession(false);
							httpSession.setAttribute("ProductView", productView);
							forward = DEFAULT;
							break;
						}
					}
				}else{
					productService = new ProductService();
					productView = productService.getProductById(salesForecastForm.getProductViewSearch().getProductId());
					httpSession.setAttribute("ProductView", productView);
					forward = DEFAULT;
				}
			}else{
				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), MESSAGE_SELECT_A_PRODUCT);
				httpSession.setAttribute(ERROR_MESSAGE, messageError);
				forward = FAILURE;
			}
		}catch (Exception e){
			SalesForecastException salesForecastException=null;
			e.printStackTrace(System.out);
			salesForecastException= new SalesForecastException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw salesForecastException;
		}
		return mapping.findForward(forward);
	}
}